﻿using AntMovement.Core;
using AntMovement.Core.Entity;
using AntMovement.Core.IParameter;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AntMovement.Menu.Device.ViewModels
{
    public class AirCylinderSystemViewModel
    {
        public ObservableCollection<string> OutputNames { get; set; }
        public ObservableCollection<string> InputNames { get; set; }
        public IOParameter IOP { get; set; }
        private IDialogHost _dialogHost;
        public AirCylinderSystemViewModel(IIOParameter iOParameter, IDialogHost dialogHost)
        {
            _dialogHost = dialogHost;
            IOP = (IOParameter)iOParameter;
            var inp = IOP.DIDataSet.Where(x => (x.Text != null) && x.Text.Contains("气缸")).Select(x => x.Text).ToList();
            var outp = IOP.DODataSet.Where(x => (x.Text != null) && x.Text.Contains("气缸")).Select(x => x.Text).ToList();
            OutputNames = new ObservableCollection<string>(outp);
            OutputNames.Add(string.Empty);
            InputNames = new ObservableCollection<string>(inp);
            InputNames.Add(string.Empty);
        }

        public DelegateCommand<string> AirCylinderCmd => new DelegateCommand<string>(sender =>
        {
            switch (sender)
            {
                case "添加":
                    IOP.AirCylinderSource.Add(new AirCylinderModel()
                    {
                        Index = IOP.AirCylinderSource.Count + 1,
                        Close_timeout = 3000,
                        Open_timeout = 3000,
                    });
                    break;

                case "保存":
                    foreach (var item in IOP.AirCylinderSource)
                    {
                        if (!string.IsNullOrEmpty(item.In_open))
                        {
                            var open = IOP.DIDataSet.FirstOrDefault(x => x.Text == item.In_open);
                            if (open != null)
                            {
                                item.In_OpenName = $"X{open.MdlIndex:D2}{open.OrderlyNum:D2}";
                            }
                        }
                        if (!string.IsNullOrEmpty(item.In_close))
                        {
                            var close = IOP.DIDataSet.FirstOrDefault(x => x.Text == item.In_close);
                            if (close != null)
                            {
                                item.In_CloseName = $"X{close.MdlIndex:D2}{close.OrderlyNum:D2}";
                            }
                        }
                        if (!string.IsNullOrEmpty(item.Close_text))
                        {
                            var close = IOP.DODataSet.FirstOrDefault(x => x.Text == item.Close_text);
                            if (close != null)
                            {
                                item.Out_CloseName = $"Y{close.MdlIndex:D2}{close.OrderlyNum:D2}";
                            }
                        }
                        if (!string.IsNullOrEmpty(item.Open_text))
                        {
                            var open = IOP.DODataSet.FirstOrDefault(x => x.Text == item.Open_text);
                            if (open != null)
                            {
                                item.Out_OpenName = $"Y{open.MdlIndex:D2}{open.OrderlyNum:D2}";
                                if (string.IsNullOrEmpty(item.Close_text))
                                {
                                    item.Out_CloseName = item.Out_OpenName;
                                }
                            }
                        }
                    }
                    IOP.Save();
                    _dialogHost.DialogShow("保存成功!!!", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
                    break;
            }
        });

        private Action<AirCylinderModel, AirCylinderModel> _copy;
        public DelegateCommand<DataGrid> InputSelectDelete => new DelegateCommand<DataGrid>(sender =>
        {
            if (_copy == null)
            {
                _copy = Core.Tool.ConversionHelper.CopyClass<AirCylinderModel, AirCylinderModel>();
            }
            if (sender.SelectedItems == null || sender.SelectedItems.Count == 0)
            {
                return;
            }
            List<AirCylinderModel> listdata = new List<AirCylinderModel>();
            foreach (AirCylinderModel item in sender.SelectedItems)
            {
                AirCylinderModel model = new AirCylinderModel();
                _copy.Invoke(item, model);
                listdata.Add(model);
            }
            foreach (AirCylinderModel item in listdata)
            {
                var model = IOP.AirCylinderSource.FirstOrDefault(x => x.Name == item.Name && x.Index == item.Index);
                if (model != null)
                {
                    IOP.AirCylinderSource.Remove(model);
                }
            }
            for (int i = 0; i < IOP.AirCylinderSource.Count; i++)
            {
                IOP.AirCylinderSource[i].Index = (ushort)(i + 1);
            }
            IOP.Save();
        });
    }
}
